package com.wz.controller;


import com.wz.pojo.Department;
import com.wz.pojo.UserInfo;
import com.wz.service.DepartmentService;
import com.wz.service.UserInfoSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;

@Controller
public class  UserInfoController {

    @Autowired
     UserInfoSerivce userInfoSerivce;
    @Autowired
     DepartmentService departmentService;

    /**展示所有员工
     * 请求路径为: GET http://localhost:8080/emp/userInfo
     * Model是为了使用 model.addAttribute，存储数据
     * 返回前端界面使用 UserInfo 里已经装载了 userInfoList信心
     */
    @RequestMapping("/user")
    public String getAllUser(Model model){
        List<UserInfo> userInfoList=userInfoSerivce.getAllUser();
        List<Department> departmentList=departmentService.AllDepartment();
        /**后台测试mapper文件中sql语句是否获得数据*/
       /* for (UserInfo userInfo : userInfoList) {
            System.out.println(userInfo);
        }
        System.out.println("部门信息："+departmentList);*/
        model.addAttribute("UserInfo",userInfoList);
        model.addAttribute("depts",departmentList);

        return "emp/tables";
    }

    /**返回json数据*/
    @RequestMapping("/users")
    @ResponseBody
    public  List<UserInfo> getAllUserList(){
        List<UserInfo> userInfoList=userInfoSerivce.getAllUser();
        return userInfoList;
    }

    /**
     * 跳转到add.html
     * 并且把部门的数据携带过去
     */
    @GetMapping("/toAdd")
    public  String toAddPage(Model model,@ModelAttribute UserInfo userInfo){
        List<Department> departmentList=departmentService.AllDepartment();
        model.addAttribute("depts",departmentList);
        return "emp/add";
    }

    /**add员工数据*/
    @PostMapping("/addUser")
    public String addUser (
            @Valid UserInfo userInfo,
            BindingResult br,
            Model model
            ) {
//获取部门id 绑定下拉框数据
            Integer departmentId=userInfo.getDepartment().getDepartmentID();
            userInfo.setDepartmentID(departmentId);
//验证数据判错
            if (br.hasErrors()){
               List<ObjectError> list=br.getAllErrors();
                for (ObjectError error : list) {
                    System.out.println(error.getDefaultMessage());
                }
// 验证失败后--跳转后再次部门赋值
                List<Department> departmentList=departmentService.AllDepartment();
                model.addAttribute("depts",departmentList);
                model.addAttribute("userInfo",userInfo);
//验证失败后--携带当前错误信息时跳转当前界面
                  return "emp/add";

            }else {
                //添加员工
                userInfoSerivce.addEmp(userInfo);
                //重新转回员工列表查询页
                return "redirect:/user";
            }

    }



    /**
     * 跳转到update.html
     * 并且把部门的数据携带过去
     */
    @GetMapping("/toUpdate/{userID}")
    public  String toUpdatePage(@PathVariable("userID")Integer userID, Model model){
    //通过UserId获取当前员工信息
    UserInfo userInfoID=userInfoSerivce.getUserById(userID);
    model.addAttribute("uIdI",userInfoID);
    /*System.out.println("UserId="+userID+"的用户信息"+userInfoID);*/

    //获取所有部门信息
    Collection<Department> departmentCollection =departmentService.AllDepartment();
    model.addAttribute("depts",departmentCollection);

        return "emp/update";
    }

    /**update员工数据*/
    @PostMapping("/update")
    public String updateUser(UserInfo userInfo){
        Integer departmentID=userInfo.getDepartment().getDepartmentID();
        userInfo.setDepartmentID(departmentID);

        userInfoSerivce.upDate(userInfo);
       System.out.println("修改后的员工信息："+userInfo);
        return"redirect:/user";
    }

    /**delete员工信息*/
    @RequestMapping("/delete/{userID}")
    public String deleteUser(@PathVariable("userID")Integer userID){
        userInfoSerivce.deleteUserById(userID);
        // System.out.println("删除ID="+userID+"的员工信息成功！");
        return"redirect:/user";
    }

    /**
     * 模糊查询
     * @return*/
    @GetMapping("/search")
    public String search(Model model,String userName) {
        List<UserInfo> userInfoList=userInfoSerivce.searchByKeyWord(userName);
            model.addAttribute("like",userInfoList);

        //获取所有部门信息
        Collection<Department> departmentCollection =departmentService.AllDepartment();
        model.addAttribute("depts",departmentCollection);

        return "emp/likeTables";
    }

        /**下拉框查询*/
        @GetMapping("/toSelectBox")
        public String selectBox(@RequestParam(value = "did") Integer did,Model model){
           List <UserInfo> userInfodid=userInfoSerivce.slsectUser(did);
            model.addAttribute("udid",userInfodid);

            //获取所有部门信息
            Collection<Department> departmentCollection =departmentService.AllDepartment();
            model.addAttribute("depts",departmentCollection);

            return "emp/SelectTables";
        }









}
